package com.global.gbinventory.barang.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.global.gbinventory.barang.tablemodel.GudangTableModel;
import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.barang.util.Status;
import com.global.gbinventory.barang.view.GudangFrame;
import com.global.gbinventory.main.controller.MainController;

public class GudangController {

	private MainController main;
	private GudangFrame frame;
	private GudangTableModel tableModel;
	
	public GudangController(MainController main){
		this.main = main;
		this.frame = new GudangFrame();
		this.tableModel = new GudangTableModel(main.getDBMaster());
		initialize();
	}
	
	public void start(){
		frame.displayFrame();
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initialize(){
		frame.disableButtonSave();
		frame.setTableModel(tableModel);
		addListeners();
	}
	
	private void addListeners(){
		frame.addBtnNewListener(new BtnNewListener());
		frame.addBtnDeleteListener(new BtnDeleteListener());
		frame.addBtnSaveListener(new BtnSaveListener());
		frame.addBtnCloseListener(new BtnCloseListener());
		
		frame.addTableListener(new TableGudangListener());
	}
	
	public String[] getAllGudangNames(){
		return tableModel.getAllGudangNames();
	}
	
	class BtnNewListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			tableModel.addNewRow();
			
			frame.enableButtonSave();
		}
		
	}
	
	class BtnDeleteListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = frame.getTableSelectedRow();
			Object kodeObj = tableModel.getValueAt(row, CONSTANT.GUDANG_TM_KODE);
			Object namaObj = tableModel.getValueAt(row, CONSTANT.GUDANG_TM_NAMA);
			Object lokasiObj = tableModel.getValueAt(row, CONSTANT.GUDANG_TM_LOKASI);
			String kode = kodeObj.toString();
			String nama = namaObj.toString();
			String lokasi = lokasiObj.toString();
			if(kodeObj == null || kode.equals("") || namaObj == null || nama.equals("")
					|| lokasiObj == null || lokasi.equals("")){
				tableModel.delete(row);
			}else{
				
				String title = "Konfirmasi Penghapusan";
				String message = "Anda yakin mau menghapus \"" + nama + " \" ?";
				int ans = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
				
				if(ans == JOptionPane.YES_OPTION){
					tableModel.delete(row);
				}
			}
			
		}
		
	}
	
	class BtnSaveListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Status status = tableModel.save();
			status.showMessage(frame);
			
			frame.disableButtonSave();
		}
		
	}
	
	class BtnCloseListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(frame.isButtonSaveEnabled()){
				String title = "Konfirmasi Save";
				String message = "Data telah berubah. Simpan data terlebih dahulu sebelum keluar?";
				int ans = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
				
				if(ans == JOptionPane.YES_OPTION){
					Status status = tableModel.save();
					status.showMessage(frame);
					
					frame.disableButtonSave();
					frame.dispose();
				}else{
					frame.dispose();
				}
			} else{
				frame.dispose();
			}
		}
		
	}
	
	class TableGudangListener implements TableModelListener{

		@Override
		public void tableChanged(TableModelEvent e) {
			int row = e.getFirstRow();
			switch (e.getType()) {
			case TableModelEvent.INSERT:
				
				break;
			case TableModelEvent.UPDATE:
				tableModel.addEditedTuple(row);
				frame.enableButtonSave();
				break;
			case TableModelEvent.DELETE:
				
				break;
			default:
				break;
			}
			
		}
		
	}
	
}
